import {
  getTxCount
} from '../../config/apiServe/new-rpc'
export default {
  data() {
    return {
      tab: 0, // 0 --> 交易记录，1 --> 持有人
      searchAddress: "",
      propsAdr: '',
      tokenName: '',
      isSearch: true,
      TokenInfo: {},
      pages: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      txCountParams: [{
        "match": [{
          "key": "options.symbol",
          "value": this.$route.query.TokenName
        }]
      }],
      txs: [],
      holders: []
    };
  },
  computed: {
    TokensList() {
      return this.$store.state.tokenList
    },
    LoadingSucc() {
      return this.$store.state.tokenListSuccess
    }
  },
  watch: {
    'TokensList'(val) {
      if (val.length > 0) this.getTokenInfos()
    }
  },
  mounted() {
    this.tokenName = this.$route.query.TokenName
    this.propsAdr = this.$route.query.adr || ''
    this.getTokenInfos()
    this.initRecord()
  },
  methods: {
    getTokenInfos() {
      if (this.TokensList.length <= 0) return
      this.TokensList.forEach(item => {
        if (item.symbol === this.tokenName) this.TokenInfo = {
          ...item,
          ...this.TokenInfo
        }
      })
    },
    searchAdr() {
      if (this.searchAddress.length < 30) {
        this.$notify({
          title: this.$t('components.tip'),
          message: this.$t('components.errAdr'),
          type: 'warning'
        })
        return
      }
      this.propsAdr = this.searchAddress
      this.searchAddress = ''
    },
    initRecord() {
      this.getTxCount()
    },
    // 获取交易记录
    async getTxCount() {
      const res = await getTxCount(this.txCountParams)
      if (res.error === null) {
        this.TokenInfo = {
          ...{
            txCount: res.result
          },
          ...this.TokenInfo
        }
      }
    },
    tabChange(tab) {
      this.tab = tab
      this.propsAdr = ''
      this.pages.currentPage = 1
      this.pages.pageSize = 10
      this.$router.push({
        path: '/tokenDetail',
        query: {
          TokenName: this.TokenInfo.symbol
        }
      })
      if (tab === 1) {
        console.log(this.TokenInfo)
        this.pages.total = this.TokenInfo.holders
      }
    },
    pageChange(page) {
      this.pages.currentPage = page;
    },
    sizeChange(size) {
      this.pages.pageSize = size;
      this.pages.currentPage = 1
    }
  }
}